package com.zhuiYun.project.api.lott.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.zhuiYun.project.api.lott.entry.Lottery;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class LotteryController {

    @GetMapping("/getNum")
    public List getNum(){
        List l = new ArrayList();
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("https://api.topthink.com/lottery/history?appCode=3d4f8f73-e45b-434c-a717-ee65e02de524&lotteryId=plw&pageSize=50&page=1",String.class);
        // String s = "{\"code\":0,\"message\":\"查询成功\",\"data\":{\"lotteryResList\":[{\"lottery_id\":\"plw\",\"lottery_res\":\"7,3,0,4,9\",\"lottery_no\":\"24184\",\"lottery_date\":\"2024-07-12\",\"lottery_exdate\":\"2024-09-11\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"5,0,0,9,5\",\"lottery_no\":\"24183\",\"lottery_date\":\"2024-07-11\",\"lottery_exdate\":\"2024-09-10\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"2,8,2,4,7\",\"lottery_no\":\"24182\",\"lottery_date\":\"2024-07-10\",\"lottery_exdate\":\"2024-09-09\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"3,2,3,9,5\",\"lottery_no\":\"24181\",\"lottery_date\":\"2024-07-09\",\"lottery_exdate\":\"2024-09-08\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"2,1,5,4,9\",\"lottery_no\":\"24180\",\"lottery_date\":\"2024-07-08\",\"lottery_exdate\":\"2024-09-07\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"5,3,5,4,4\",\"lottery_no\":\"24179\",\"lottery_date\":\"2024-07-07\",\"lottery_exdate\":\"2024-09-06\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"6,6,3,4,7\",\"lottery_no\":\"24178\",\"lottery_date\":\"2024-07-06\",\"lottery_exdate\":\"2024-09-05\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"5,7,9,8,2\",\"lottery_no\":\"24177\",\"lottery_date\":\"2024-07-05\",\"lottery_exdate\":\"2024-09-04\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"9,3,2,5,0\",\"lottery_no\":\"24176\",\"lottery_date\":\"2024-07-04\",\"lottery_exdate\":\"2024-09-03\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"},{\"lottery_id\":\"plw\",\"lottery_res\":\"1,9,5,0,6\",\"lottery_no\":\"24175\",\"lottery_date\":\"2024-07-03\",\"lottery_exdate\":\"2024-09-02\",\"lottery_sale_amount\":\"\",\"lottery_pool_amount\":\"\"}],\"page\":1,\"pageSize\":10,\"totalPage\":695}}\n";
        JSONObject map = JSONObject.parseObject(response);
        JSONObject jsonObject = (JSONObject)map.get("data");
        String o = jsonObject.get("lotteryResList").toString();
        List<Lottery> list = JSON.parseArray(o, Lottery.class);
        // 自组号码
        HashMap<String,String> h = new HashMap<>();
        h.put("0","4961549615");
        h.put("1","6072760727");
        h.put("2","1194811948");
        h.put("3","8855488554");
        h.put("4","3216932169");
        h.put("5","9387693876");
        h.put("6","5428054280");
        h.put("7","0509105091");
        h.put("8","7633276332");
        h.put("9","2740327403");
        // 这两个值是用来获取前两期中的数使用的
        int oneIndex = 0;
        int twoIndex = 0;
        int startIndex = 5;
        for (int i=list.size()-3;i>=0;i--){
            oneIndex = i+2;
            twoIndex = i+1;
            Lottery lotteryOne = list.get(oneIndex);
            Lottery lotteryTwo = list.get(twoIndex);
            Lottery lottery = list.get(i);
            // 通过彩票第一位数 获取自组号码中的数据
            String  one = lotteryOne.getLottery_res().split(",")[0];
            String  two = lotteryTwo.getLottery_res().split(",")[0];
            String  three = lottery.getLottery_res().split(",")[0];
            String oneNum = h.get(one);
            String twoNum = h.get(two);
            String threeNum = h.get(three);
            startIndex+=1;
            char oneChar = oneNum.charAt(startIndex);
            startIndex-=1;
            char twoChar = twoNum.charAt(startIndex);
            startIndex-=1;
            char threeChar = threeNum.charAt(startIndex);
            if(startIndex == 0){
                startIndex = 5;
            }
            l.add(threeChar+""+twoChar+oneChar);
        }
        return l;
    }
}
